package com.example.springbook.service;


import com.example.springbook.enums.BookStatus;
import com.example.springbook.mapper.BookMapper;
import com.example.springbook.model.BookInfo;
import com.example.springbook.model.PageRequest;
import com.example.springbook.model.PageResult;
import com.example.springbook.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public Integer insertBook(BookInfo bookInfo){
        return bookMapper.insert(bookInfo);
    }

    public PageResult<BookInfo> getBookListBypage(PageRequest pageRequest) {
        Integer count=bookMapper.count();
        List<BookInfo> bookInfos=bookMapper.queryBookByPage(pageRequest.getOffest(), pageRequest.getPageSize());
        for (BookInfo bookInfo:bookInfos){
//            if (bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }else {
//                bookInfo.setStatusCN("不可借阅");
//            }
            bookInfo.setStatusCN(BookStatus.getDescBycode(bookInfo.getStatus()).getDesc());
        }
        return new PageResult<>(bookInfos,count,pageRequest);
    }

    public BookInfo getBookById(Integer id) {

        BookInfo bookInfo= bookMapper.queryBookById(id);
        log.info("Bookinfo:",bookInfo);
        return bookInfo;
    }
    public String updateBook(BookInfo bookInfo) {
        try {
            Integer result=bookMapper.updateBook(bookInfo);
            if(result>0){
                return "";
            }
        }catch (Exception e){
            return "更新失败";
        }
        return "更新失败";
    }

    public String deleteBook(BookInfo bookInfo) {
    bookInfo.setStatus(0);
        try {
            Integer result=bookMapper.updateBook(bookInfo);
            if(result>0){
                return "";
            }
        }catch (Exception e){
            return "删除失败";
        }
        return "删除失败";
    }

    public String batchDeleteBookByIds(List<Integer> ids) {

            log.info("ids:{}"+ids);
            Integer result=bookMapper.batchDeleteBookByIds(ids);
            if(result>0) {
                return "";
            } else
                return "删除失败";
    }
}
